package cn.yh.hospitalappointmentsystemdemo.controller;

import cn.yh.hospitalappointmentsystemdemo.common.AjaxResult;
import cn.yh.hospitalappointmentsystemdemo.common.TableDataInfo;
import cn.yh.hospitalappointmentsystemdemo.controller.base.BaseController;
import cn.yh.hospitalappointmentsystemdemo.domain.entity.PreAppointments;
import cn.yh.hospitalappointmentsystemdemo.domain.entity.RegistrationOrders;
import cn.yh.hospitalappointmentsystemdemo.domain.query.OrdersCreate;
import cn.yh.hospitalappointmentsystemdemo.domain.query.OrdersQuery;
import cn.yh.hospitalappointmentsystemdemo.domain.query.PageQuery;
import cn.yh.hospitalappointmentsystemdemo.domain.vo.PreAppointmentVo;
import cn.yh.hospitalappointmentsystemdemo.domain.vo.RegistrationOrdersVo;
import cn.yh.hospitalappointmentsystemdemo.mapping.AppointmentMapping;
import cn.yh.hospitalappointmentsystemdemo.mapping.OrdersMapping;
import cn.yh.hospitalappointmentsystemdemo.service.IAppointmentService;
import cn.yh.hospitalappointmentsystemdemo.service.IOrdersService;
import cn.yh.hospitalappointmentsystemdemo.utils.JWTUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "预约管理")
@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController {
    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IAppointmentService appointmentService;

    @ApiOperation(value = "创建预约")
    @PostMapping
    public AjaxResult createOrder(@RequestBody OrdersQuery ordersQuery){
        LambdaQueryWrapper<PreAppointments> lqw = Wrappers.lambdaQuery();
        lqw.eq(PreAppointments::getPreAppointmentId,ordersQuery.getPreAppointmentId());
        PreAppointments one = appointmentService.getOne(lqw);
        System.out.println("One: "+one);
        OrdersCreate ordersCreate = new OrdersCreate();
        ordersCreate.setDoctorId(JWTUtils.getUserIdByClaim());
        ordersCreate.setRegistrationTime(new Date());
//        ordersCreate.setAppointmentTime(one.getPreAppointmentTime());
        ordersCreate.setAppointmentDate(one.getPreDate());
        ordersCreate.setTimeSlot(one.getTimeSlot());
        ordersCreate.setPatientId(one.getPatientId());
        ordersCreate.setDepartmentId(one.getDepartmentId());
        System.out.println(ordersCreate);
        System.out.println(OrdersMapping.INSTANCE.toCreate(ordersCreate));
        return toAjax(ordersService.save(OrdersMapping.INSTANCE.toCreate(ordersCreate)));
    }

    @GetMapping("/list")
    public TableDataInfo list(PageQuery pageQuery) {
//        LambdaQueryWrapper<RegistrationOrders> lqw = Wrappers.lambdaQuery();
//        lqw.eq(RegistrationOrders::getPatientId,JWTUtils.getUserIdByClaim());
//        Page<RegistrationOrders> page = ordersService.page(new Page<>(pageQuery.getPageNum(),pageQuery.getPageSize()),lqw);
//        Page<RegistrationOrdersVo> result = OrderMapping.INSTANCE.toPage(page);
        Page page = ordersService.page(pageQuery, JWTUtils.getUserIdByClaim());
        return dataInfo(page.getRecords(),page.getTotal());
    }
}
